package com.iptv.grape.ui.control.ad;

import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.IListViewShowItem;
import com.iptv.grape.util.AppLog;

import java.util.ArrayList;
import java.util.List;

public class ListViewUIList {

    private int curFocusIndex = 0;

    public int getCurFocusIndex() {
        return curFocusIndex;
    }



    public void setCurFocusIndex(int curFocusIndex) {
        this.curFocusIndex = curFocusIndex;
        for(int i = 0; i < listViewUIList.size(); i++){

            if(i <= this.curFocusIndex){
                listViewUIList.get(i).show();
            }

            if(!(this.curFocusIndex + 1>= i)){
                listViewUIList.get(i).hide();
            }

        }
    }

    public void reSetFocus(){
        listViewUIList.get(getCurFocusIndex()).requestFocus();
        //AppLog.getCurLog().out(getCurFocusIndex()+"");
    }

    private List<ListViewUI> listViewUIList = new ArrayList<ListViewUI>();

    public int size(){
        return this.listViewUIList.size();
    }

    public void add(ListViewUI listViewui){
        if(listViewUIList.size() == 0){
            // 第一个元素
            listViewui.show();
            listViewui.refresh(GrapeContext.curGrapeContext().getTopListViewShowItem());
        }
        else {
            listViewui.hide();
        }
        listViewUIList.add(listViewui);
        listViewui.setIndex(listViewUIList.size() - 1);
        listViewui.setMian(this);
        if(listViewUIList.size() - 1 > 0){
            listViewUIList.get(listViewUIList.size() - 2).setNextListViewUI(listViewui);
        }

    }

    public ListViewUI get(int index){
        return listViewUIList.get(index);
    }

    public void start(){
        ListViewUI listUI = this.get(0);
        startHelp(listUI);
    }

    private void startHelp(ListViewUI listUI){
        if(listUI.hasNextListViewUI()){
            IListViewShowItem item = listUI.getAdapter().getListViewShowItem(listUI.getListViewVScroll().getFocusIndex());
            //AppLog.getCurLog().out(listUI.getListViewVScroll().getFocusIndex() + "");
            List<IListViewShowItem> chList = item.getChList();
            if(chList != null && chList.size() > 0){
                listUI.getNextListViewUI().show();
                listUI.getNextListViewUI().refresh(chList);
                listUI.requestFocus();
                startHelp(listUI.getNextListViewUI());
            }
        }
    }
}
